#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''简单的协程'''
def receiver():
    print("ready to receive")
    while True:
        msg = yield
        print("Get %s"%msg)

r = receiver()
'''必须要发送send(None)或者next()才能开启一个协程'''
r.send(None)
r.send("hello")
r.send("world")

'''使用装饰器，自动开启协程'''
def coroutine(func):
    def start(*args, **kwargs):
        g = func(*args, **kwargs)
        next(g)
        return g
    return start
@coroutine
def receiver2():
    print("ready to receive")
    while True:
        msg = yield
        print("Get %s"%msg)

r2 = receiver2()
r2.send('gateray')
r2.send('zhou')